package com.cashoutgaming.client.modules.toolbar;

import com.cashoutgaming.client.common.SessionManager;
import com.cashoutgaming.client.gin.NameTokens;
import com.cashoutgaming.client.modules.shell.DesktopShellPresenter;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
import com.gwtplatform.mvp.client.HasUiHandlers;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyCodeSplit;
import com.gwtplatform.mvp.client.annotations.Title;
import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.gwtplatform.mvp.client.proxy.PlaceRequest;
import com.gwtplatform.mvp.client.proxy.ProxyPlace;

public class ApplicationToolbarPresenter extends Presenter<ApplicationToolbarPresenter.MyView, ApplicationToolbarPresenter.MyProxy> implements
		ApplicationToolbarUiHandlers {

	private final PlaceManager placeManager;
	private SessionManager session;
	private String currentUserId;

	@ProxyCodeSplit
	@NameToken(NameTokens.MAIN_TOOLBAR)
	@Title("Application Toolbar")
	public interface MyProxy extends ProxyPlace<ApplicationToolbarPresenter> {
	}

	public interface MyView extends View, HasUiHandlers<ApplicationToolbarUiHandlers> {

		public void updateUser(String username);

		public void refreshLayout();

		public void setUserId(Long id);
	}

	@Inject
	ApplicationToolbarPresenter(EventBus eventBus, MyView view, MyProxy proxy, PlaceManager placeManager, SessionManager session) {
		super(eventBus, view, proxy, DesktopShellPresenter.TYPE_TOOLBAR);
		this.placeManager = placeManager;
		this.session = session;
		view.setUiHandlers(this);
	}

	@Override
	public void prepareFromRequest(PlaceRequest request) {
		super.prepareFromRequest(request);
	}

	public void revealInitPlace(String token) {
		PlaceRequest request = new PlaceRequest.Builder().nameToken(token).build();
		placeManager.revealPlace(request);
	}

	public void logout() {
		// service.logout(new MethodCallback<Void>() {
		//
		// @Override
		// public void onFailure(Method method, Throwable exception) {
		// session.clearSession();
		// }
		//
		// @Override
		// public void onSuccess(Method method, Void response) {
		// session.clearSession();
		// }
		// });
	}

	@Override
	public void onReveal() {
		if (currentUserId == null) {

		}
		String username = "Login";
		if (session != null && session.getUser() != null) {
			if (session.getUser().getDisplay_name() != null && session.getUser().getDisplay_name().length() > 0) {
				username = session.getUser().getDisplay_name();
			}
			else {
				username = session.getUser().getUsername();
			}
		}
		getView().updateUser(username);
		// getView().setUserId(session.getUser().getId());TODO Uncomment after
		// wire-up
		getView().setUserId(0L);
		getView().refreshLayout();
	}

	@Override
	public void signout() {

	}

	@Override
	public String getUserId() {
		String username = "Login";
		if (session == null || session.getUser() == null) {
			return null;
		}
		if (session.getUser().getDisplay_name() != null && session.getUser().getDisplay_name().length() > 0) {
			username = session.getUser().getDisplay_name();
		}
		else {
			username = session.getUser().getUsername();
		}
		return username;
	}

	@Override
	public void showUserProfile(Long id) {
		PlaceRequest newRequest = new PlaceRequest.Builder().nameToken(NameTokens.USER_PROFILE).with("id", id.toString()).build();
		placeManager.revealPlace(newRequest, true);
	}

	@Override
	public void goHome() {
		PlaceRequest newRequest = new PlaceRequest.Builder().nameToken(NameTokens.HOME).build();
		placeManager.revealPlace(newRequest, true);
	}
}
